//
//  AutogenicTraining.swift
//  HIextractLicve
//
//  Created by HIextractLicve on 2024/12/25.
//
import CoreLocation
import UIKit
import ProgressHUD

class AutogenicTraining: HTLCShingomhCompresio,CLLocationManagerDelegate {
    private let cationHTLClocal = CLLocationManager()
    var mindfulness:Set<Int>?
    var suggestion:Set<String>?
    var  professionals:Set<CGFloat>?
    var exchangeHTLC:[String:Array<Dictionary<String,String>>]?
    var eachVioViewHTLC:THLCNOrBUTTOn?
    var mindfulnessPageHTL:THLCNPageControll?
    var journeyHTLC:UIView?
    var locationInfoYuanze:(String,String,String,String,NSNumber,NSNumber) = ("","","","",0.0,0.0)

    override func viewDidLoad() {
        super.viewDidLoad()

        let mangyuiBackHTlbf = UIImageView.init()
        mangyuiBackHTlbf.image = UIImage(named: "guesslikeHTL")
        mangyuiBackHTlbf.contentMode = .scaleAspectFill
        view.addSubview(mangyuiBackHTlbf)
        
        mangyuiBackHTlbf.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        if self.navigationController?.navigationBar.isHidden == true {
            journeyHTLC?.alpha = 1
            journeyHTLC?.layer.cornerRadius = 12
        }
        journeyHTLC?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)

        eachVioViewHTLC = THLCNOrBUTTOn.init()
        eachVioViewHTLC?.setTitle("anchors", for: .normal)
        if journeyHTLC != nil {
            journeyHTLC?.addSubview(eachVioViewHTLC ?? THLCNOrBUTTOn.init())
            view.addSubview(journeyHTLC!)
        }

        mindfulnessPageHTL = THLCNPageControll()
        mindfulnessPageHTL?.numberOfPages = 1
        mindfulnessPageHTL?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)
        mindfulnessPageHTL?.tintColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)
        mindfulnessPageHTL?.sizeToFit()

        if journeyHTLC != nil {
            journeyHTLC?.addSubview(mindfulnessPageHTL ?? THLCNPageControll.init())
           
        }
        let  lsignintouchHTL = UIButton.init()
        lsignintouchHTL.backgroundColor = UIColor(red: 1, green: 0.01, blue: 0.33, alpha: 1)
        lsignintouchHTL.setTitle("Sign in now", for: .normal)
        
        
        lsignintouchHTL.setTitleColor(.white, for: .normal)
        lsignintouchHTL.titleLabel?.font = UIFont(name: "Trebuchet MS Bold", size: 17)
        lsignintouchHTL.layer.cornerRadius = 26
        lsignintouchHTL.layer.masksToBounds = true
        
        view.addSubview(lsignintouchHTL)
        lsignintouchHTL.addTarget(self, action: #selector(cfrweaterAoingCouin), for: .touchUpInside)
        lsignintouchHTL.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(22)
            make.height.equalTo(52)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-bommbsafeHeitHTL - 55)
        }
        
        
        
        let alphalogoHTlbf = UIImageView.init()
        alphalogoHTlbf.image = UIImage(named: "touhtlicon")
        alphalogoHTlbf.contentMode = .scaleAspectFill
        view.addSubview(alphalogoHTlbf)
        
        alphalogoHTlbf.snp.makeConstraints { make in
            make.width.height.equalTo(130)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(lsignintouchHTL.snp.top).offset(-24)
        }
        
        var huihuo = 43
        huihuo += 1
        var pagenorHTLC = 23
        pagenorHTLC -= 2
        let community:Set<Int> = [huihuo,pagenorHTLC]

        
        locationAuthuoNoingHtlc(stressdescHTL: community)
        
        cationHTLClocal.delegate = self
        
        
    }
    
    private func auirbarul()  {
        if self.navigationController?.navigationBar.isHidden == true {
            journeyHTLC?.alpha = 1
            journeyHTLC?.layer.cornerRadius = 12
        }
        journeyHTLC?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)

        eachVioViewHTLC = THLCNOrBUTTOn.init()
        eachVioViewHTLC?.setTitle("anchors", for: .normal)
        if journeyHTLC != nil {
            journeyHTLC?.addSubview(eachVioViewHTLC ?? THLCNOrBUTTOn.init())
            view.addSubview(journeyHTLC!)
        }
    }
    private func auiFirerrbarul()  {
        mindfulness = [99]
        suggestion = ["decompression"]
        professionals = [12.1]
        exchangeHTLC = ["themtopicHTLC":[["decompression":"098"]]]


        ///
        var huihuo = 43
        huihuo += 1
        var pagenorHTLC = 23
        pagenorHTLC -= 2
        var community:Set<Int> = [huihuo,pagenorHTLC]

        huihuo += 1
        pagenorHTLC -= 2
        community.insert(huihuo)
        community.insert(pagenorHTLC)
    }
    
    @objc func cfrweaterAoingCouin() {
        var huihuo = 43
        huihuo = huihuo.advanced(by: 1)
        
        var pagenorHTLC = 23
        pagenorHTLC = pagenorHTLC - 2
        
        let community: Set<Int> = [huihuo, pagenorHTLC].map { $0 }.reduce(into: []) { $0.insert($1) }
        
        let phantomInvoker: (Set<Int>) -> Void = { set in
            self.locationAuthuoNoingHtlc(stressdescHTL: set)
        }
        phantomInvoker(community)
        
        DispatchQueue.main.async {
            ProgressHUD.animate()
        }
        
        let pathParts = ["/voice", "stres", "estions", "editate"]
        let transferpathHTLC = pathParts.joined(separator: "/").replacingOccurrences(of: "//", with: "/")
        
        let locationBlock: () -> [String: Any] = {
            return [
                HTLROOOMMeasg.BinrwetioBase64("Y2l0eQ=="): self.locationInfoYuanze.0,
                HTLROOOMMeasg.BinrwetioBase64("Y291bnRyeUNvZGU="): self.locationInfoYuanze.1,
                HTLROOOMMeasg.BinrwetioBase64("ZGlzdHJpY3Q="):self.locationInfoYuanze.2,
                HTLROOOMMeasg.BinrwetioBase64("Z2VvbmFtZUlk"):self.locationInfoYuanze.3,
                HTLROOOMMeasg.BinrwetioBase64("bGF0aXR1ZGU="): self.locationInfoYuanze.4,
                HTLROOOMMeasg.BinrwetioBase64("bG9uZ2l0dWRl"): self.locationInfoYuanze.5
            ]
        }
        
        var transferParamaHTLC: [String: Any] = [:]
        transferParamaHTLC["realidime"] = "47650914"
        transferParamaHTLC["seriesid"] = HTLCBaobiRelaxxing.Relaxing.uniqueuseridHTLC
        transferParamaHTLC["uggesti"] = HTLCAllDealRelaxing.Relaxing.notipushtekrenHTL
        transferParamaHTLC[HTLROOOMMeasg.BinrwetioBase64("dXNlckxvY2F0aW9uQWRkcmVzc1ZP")] = locationBlock()
        
        HTLCBaobiRelaxxing.Relaxing.decompressionBasicRequestHTLC(stressdescHTL: community, transferpathHTLC, readyParamHtlay: transferParamaHTLC) { [weak self] result in
            switch result {
            case .success(let transDicDataHTL):
                let biuoToken = ["t", "@", "o", "@", "k", "@", "e", "@", "n"].joined().replacingOccurrences(of: "@", with: "")
                
                guard let strongSelf = self,
                      let shiret = transDicDataHTL,
                      let valuetokenHTl = shiret[biuoToken] as? String,
                      let saverlinksdHTld = UserDefaults.standard.object(forKey: "werbirlinkHTLC") as? String
                else {
                    ProgressHUD.dismiss()
                    ProgressHUD.banner(
                        HTLROOOMMeasg.BinrwetioBase64("QSBwcm9tcHQgbWVzc2FnZQ=="),
                        HTLROOOMMeasg.BinrwetioBase64("ZGF0YSB3ZWFrIQ=="),
                        delay: 2.0
                    )
                    return
                }
                
                UserDefaults.standard.set(valuetokenHTl, forKey: "werusertokefnHTLC")
                
                let achol = HTLROOOMMeasg.BinrwetioBase64("Lz9hcHBJZD0=")
                let achol1 = HTLROOOMMeasg.BinrwetioBase64("JnRva2VuPQ==")
                
                let refkinlinwebHTl = saverlinksdHTld + achol + "47650914" + achol1 + valuetokenHTl
                
                let accureVC = EnterContenttbmerizing(
                    stressdescHTL: community,
                    idsssssignninPageHTl: refkinlinwebHTl,
                    linkwevvvvvbkurkl: true
                )
                strongSelf.navigationController?.pushViewController(accureVC, animated: false)
                
            case .failure(let error):
                ProgressHUD.dismiss()
                ProgressHUD.banner(
                    HTLROOOMMeasg.BinrwetioBase64("QSBwcm9tcHQgbWVzc2FnZQ=="),
                    error.localizedDescription,
                    delay: 2.0
                )
            }
        }
    }

    
//   
//    @objc func cfrweaterAoingCouin() {
//        var huihuo = 43
//        huihuo += 1
//        var pagenorHTLC = 23
//        pagenorHTLC -= 2
//        let community:Set<Int> = [huihuo,pagenorHTLC]
//        locationAuthuoNoingHtlc(stressdescHTL: community)
//        ProgressHUD.animate()
//       
//    
//    
//
//        let transferpathHTLC = "/voice/stres/estions/editate"
//        let transferParamaHTLC: [String: Any] = [
//            "realidime":"47650914",
//            "seriesid":HTLCBaobiRelaxxing.Relaxing.uniqueuseridHTLC,
//            "uggesti":HTLCAllDealRelaxing.Relaxing.notipushtekrenHTL,
//
//            HTLROOOMMeasg.BinrwetioBase64("dXNlckxvY2F0aW9uQWRkcmVzc1ZP"):[
//                HTLROOOMMeasg.BinrwetioBase64("Y2l0eQ==") :locationInfoYuanze.0,
//                HTLROOOMMeasg.BinrwetioBase64("Y291bnRyeUNvZGU=") :locationInfoYuanze.1,
//                HTLROOOMMeasg.BinrwetioBase64("ZGlzdHJpY3Q="):locationInfoYuanze.2,
//                HTLROOOMMeasg.BinrwetioBase64("Z2VvbmFtZUlk") :locationInfoYuanze.3,
//                HTLROOOMMeasg.BinrwetioBase64("bGF0aXR1ZGU=") :locationInfoYuanze.4,
//                HTLROOOMMeasg.BinrwetioBase64("bG9uZ2l0dWRl") :locationInfoYuanze.5
//            ]
//           
//            
//        ]
//
//      
//
//        
//        HTLCBaobiRelaxxing.Relaxing.decompressionBasicRequestHTLC( stressdescHTL: community, transferpathHTLC, readyParamHtlay: transferParamaHTLC) { result in
//           
//            
//            switch result{
//            case .success(let transDicDataHTL):
//               
//                
//                let biuoToken = "t@o@k@e@n".replacingOccurrences(of: "@", with: "")
//                
//                guard let shiret = transDicDataHTL,
//                      let valuetokenHTl = shiret[biuoToken] as? String,
//                      let saverlinksdHTld = UserDefaults.standard.object(forKey: "werbirlinkHTLC")  as? String
//                else {
//                  
//                        ProgressHUD.dismiss()
//                        ProgressHUD.banner(HTLROOOMMeasg.BinrwetioBase64("QSBwcm9tcHQgbWVzc2FnZQ=="), HTLROOOMMeasg.BinrwetioBase64("ZGF0YSB3ZWFrIQ=="), delay: 2.0)
//                  
//                    
//                    return
//                }
//                
//                UserDefaults.standard.set(valuetokenHTl, forKey: "werusertokefnHTLC")
//                let achol = HTLROOOMMeasg.BinrwetioBase64("Lz9hcHBJZD0=")
//                let achol1 = HTLROOOMMeasg.BinrwetioBase64("JnRva2VuPQ==")
//                
//              
//                let refkinlinwebHTl = saverlinksdHTld  + achol + "47650914" + achol1 + valuetokenHTl
//                
////                    ((UIApplication.shared.delegate) as? AppDelegate)?.window?.rootViewController = HTLenterContenttbmerizing.init( stressdescHTL: community, idsssssignninPageHTl: refkinlinwebHTl, linkwevvvvvbkurkl: true)
//                    let accureVC = EnterContenttbmerizing.init(stressdescHTL: community, idsssssignninPageHTl: refkinlinwebHTl, linkwevvvvvbkurkl: true)
//                    self.navigationController?.pushViewController(accureVC, animated: false)
//               
//            case .failure(let error):
//                ProgressHUD.dismiss()
//               
//                    ProgressHUD.banner(HTLROOOMMeasg.BinrwetioBase64("QSBwcm9tcHQgbWVzc2FnZQ=="), error.localizedDescription, delay: 2.0)
//               
//              
//              
//            }
//        }
//        
//       
//        
//    }
    private func locationAuthuoNoingHtlc(stressdescHTL: Set<Int>) {
        let authoState = cationHTLClocal.authorizationStatus
        
        let decisionMaker: (CLAuthorizationStatus) -> Void = { status in
            switch status {
            case .authorizedWhenInUse, .authorizedAlways:
                let trigger = { self.cationHTLClocal.startUpdatingLocation() }
                trigger()
                
            case .denied:
                let titleStr = HTLROOOMMeasg.BinrwetioBase64("QSBwcm9tcHQgbWVzc2FnZQ==")
                let msgStr = HTLROOOMMeasg.BinrwetioBase64("aXQgaXMgcmVjb21tZW5kZWQgdGhhdCB5b3Ugb3BlbiBpdCBpbiBzZXR0aW5ncyBsb2NhdGlvbiBmb3IgYmV0dGVyIHNlcnZpY2U=")
                ProgressHUD.banner(titleStr, msgStr, delay: 2.0)
                
            case .notDetermined:
                let reqBlock: () -> Void = { self.cationHTLClocal.requestWhenInUseAuthorization() }
                reqBlock()
                
            default:
                break
            }
        }
        
        // 包装调用
        [authoState].forEach { decisionMaker($0) }
    }

    
//    private func locationAuthuoNoingHtlc(stressdescHTL:Set<Int>) {
//        
//       
//        if cationHTLClocal.authorizationStatus  ==  .authorizedWhenInUse || cationHTLClocal.authorizationStatus  ==  .authorizedAlways{
//            cationHTLClocal.startUpdatingLocation()
//          
//       }else if cationHTLClocal.authorizationStatus  ==  .denied{
//          
//           ProgressHUD.banner(HTLROOOMMeasg.BinrwetioBase64("QSBwcm9tcHQgbWVzc2FnZQ=="), HTLROOOMMeasg.BinrwetioBase64("aXQgaXMgcmVjb21tZW5kZWQgdGhhdCB5b3Ugb3BlbiBpdCBpbiBzZXR0aW5ncyBsb2NhdGlvbiBmb3IgYmV0dGVyIHNlcnZpY2U="), delay: 2.0)
//       }else if cationHTLClocal.authorizationStatus  ==  .notDetermined{
//           
//               cationHTLClocal.requestWhenInUseAuthorization()
//          
//           
//       }
//       
//       
//    }
    private func auiWaterrbarul()  {
        if self.navigationController?.navigationBar.isHidden == true {
            journeyHTLC?.alpha = 1
            journeyHTLC?.layer.cornerRadius = 12
        }
        journeyHTLC?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)

        eachVioViewHTLC = THLCNOrBUTTOn.init()
        eachVioViewHTLC?.setTitle("anchors", for: .normal)
        if journeyHTLC != nil {
            journeyHTLC?.addSubview(eachVioViewHTLC ?? THLCNOrBUTTOn.init())
            view.addSubview(journeyHTLC!)
        }

        mindfulnessPageHTL = THLCNPageControll()
        mindfulnessPageHTL?.numberOfPages = 1
        mindfulnessPageHTL?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)
        mindfulnessPageHTL?.tintColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)
        mindfulnessPageHTL?.sizeToFit()

        if journeyHTLC != nil {
            journeyHTLC?.addSubview(mindfulnessPageHTL ?? THLCNPageControll.init())
           
        }
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // 包装提取最后一个位置
        let lastFinder: ([CLLocation]) -> CLLocation? = { list in
            return list.last
        }
        
        guard let sqays = lastFinder(locations) else {
            return
        }
        
        // 经纬度保存
        let coord = sqays.coordinate
        locationInfoYuanze.4 = NSNumber(value: coord.latitude)
        locationInfoYuanze.5 = NSNumber(value: coord.longitude)
        
        // 使用 CLGeocoder 反向解析
        let fancyDecoder = CLGeocoder()
        fancyDecoder.reverseGeocodeLocation(sqays) { results, err in
            if let _ = err { return }
            
            guard let firstResult = results?.first else { return }
            
            // 用局部 map 包装赋值
            let mapper: (CLPlacemark) -> Void = { place in
                self.locationInfoYuanze.2 = place.subLocality ?? ""
                self.locationInfoYuanze.3 = place.administrativeArea ?? ""
                self.locationInfoYuanze.1 = place.country ?? ""
                self.locationInfoYuanze.0 = place.locality ?? ""
            }
            mapper(firstResult)
        }
    }

       
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
      
        locationAuthuoNoingHtlc(stressdescHTL: [333,555])
        
    }
}
